﻿using EkStudio.com.bean.xml.reflection;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Field;
using System;
using System.Collections.Generic;

namespace EkStudio.com.bean.xml.BillDictionary
{
    public class BillDicModuleFieldsRoot : BillUiFormatContainer
    {

        private int maxid = 0;

        private List<BillUiFieldsItem> fields;

        private BillDicModuleItem parent = null;


        [Xml]
        public int Maxid
        {
            get { return maxid; }
            set { maxid = value; }
        }

        [XmlElement]
        public List<BillUiFieldsItem> Fields
        {
            get { return fields; }
            set { fields = value; }
        }


        [XmlTransient]
        public object ParentBean
        {
            get { return parent; }
            set { if (value is BillDicModuleItem) parent = value as BillDicModuleItem; }
        }

        public object Clone()
        {
            BillDicModuleFieldsRoot clone = new BillDicModuleFieldsRoot();
            clone.Maxid = Maxid;
            clone.Fields = new List<BillUiFieldsItem>();
            if(Fields!=null&&Fields.Count > 0)
            {
                foreach(var fd in Fields)
                {
                    BillUiFieldsItem node = (BillUiFieldsItem)fd.Clone();
                    clone.Fields.Add(node);
                }
            }
            return clone;
        }

        #region 窗口功能实现

        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            if (fields == null) fields = new List<BillUiFieldsItem>();
            BillUiFieldsItem field = node as BillUiFieldsItem;
            field.LanguageTexts = this.getNLanguageText(format);
            this.fields.Add((BillUiFieldsItem)node);
        }

        public void removeNode(BillUiFormatNode node)
        {
            BillUiFieldsItem field = node as BillUiFieldsItem;
            if (this.fields.Contains(field))
            {
                this.fields.Remove(field);
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            BillUiFieldsItem group = node as BillUiFieldsItem;
            int index = this.fields.IndexOf(group);
            if (index != -1)
            {
                int PevIndex = index - 1;
                //logger.Info("移动前当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");

                this.fields.RemoveAt(index);
                this.fields.Insert(PevIndex, group);
                //logger.Info("移动后当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");
            }
        }

        public void moveDown(BillUiFormatNode node)
        {
            BillUiFieldsItem group = node as BillUiFieldsItem;
            int index = this.fields.IndexOf(group);
            if (index < (this.fields.Count - 1))
            {
                int LastIndex = index + 1;
                BillUiFieldsItem LastGroup = this.fields[LastIndex];
                this.fields.RemoveAt(LastIndex);
                this.fields.Insert(index, LastGroup);
            }
        }

        public string getNLanguageText(BillFormatModule format)
        {
            string result = "";
            if (format != null)
            {
                int index = format.getLanguageText().IndexOf("Caption");
                if (index != -1)
                {
                    int length = format.getLanguageText().Length - 7;
                    result = format.getLanguageText().Substring(0, length);
                    int tempMaxid = maxid + 1;
                    this.Maxid = tempMaxid;
                    result = result + "fields[" + tempMaxid + "].Caption";
                }
            }
            
            return result;
        }

        public void updateLanguageText(BillFormatModule format)
        {
            if (this.fields != null && this.fields.Count > 0)
            {
                this.maxid = 0;
                foreach (BillUiFieldsItem field in this.fields)
                {
                    field.LanguageTexts = this.getNLanguageText(format);
                }
            }
        }

        #endregion
    }
}
